package com.hns.phone.safety.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.common.aop.sedate.SedateAnnotation;
import com.hns.can.common.domain.GridPage;
import com.hns.can.common.domain.ReturnResult;
import com.hns.can.common.formula.FillCodeDscValue;
import com.hns.can.common.service.CommonInitParamService;
import com.hns.can.common.web.BaseController;
import com.hns.frame.mybatis.Pagination;
import com.hns.phone.safety.service.PhoneDrivingBehaviorService;
import com.hns.phone.safety.vo.DrivingBehaviorParamVo;
import com.hns.tool.pub.PubUtil;

@Controller
@RequestMapping("/api/phoneDrivingBhv")
public class PhoneDrivingBehaviorController extends BaseController {
	
	@Autowired
    protected CommonInitParamService commonInitParamService;
	
	@Autowired
    private PhoneDrivingBehaviorService phoneDrivingBehaviorService;//驾驶行为service
	
	@Autowired
    private FillCodeDscValue codeDscValue;
	
	/**
	 * 驾驶行为列表(手机)
	 * @param entity
	 * @param request
	 * @param response
	 * @throws Exception
	 * @author:zhangyu
	 * @email:zhangyu2222z@sina.cn
	 */
    @SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("/getDrivingBhvListForMobile")
    @ResponseBody
    public Object getDrivingBhvListForMobile(DrivingBehaviorParamVo entity,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
    	ReturnResult reslutVo = new ReturnResult("0", false);
    	try{
    		//如果传机构id，则获取用户拥有资源权限
    		commonInitParamService.getResourcePmsByUser(entity,request);
    		commonInitParamService.setOrgIds(entity, null, this.getPhoneCurrentUser(), null);
    		//设置机构id为null
    		entity.setOrganId(null);
    		//初始化开始结束时间
    		commonInitParamService.dateType2DateTime(entity, entity.getTimeType());
    		
    		if(PubUtil.isEmpty(entity.getType())){
    			entity.setType("CAR");
    		}
    		Pagination page = phoneDrivingBehaviorService.getDrivingBehavior(entity, request);
        	List list = page.getData();
        	Map<String,String> mapTmp = new HashMap<String,String>();
            mapTmp.put("drvSctBhv", "DRV00000");
            codeDscValue.fillCodeDscValue(list, mapTmp);
            page.setData(list);
        	GridPage grid = new GridPage(page);
    		reslutVo.setMsg("success");
        	reslutVo.setMsgType("1");
        	reslutVo.setObj(grid);
    	}catch(Exception e){
    		reslutVo.setMsg("failed");
        	reslutVo.setMsgType("0");
        	reslutVo.setObj(null);
        	e.printStackTrace();
    	}
//        ResponseUtil.responseJSON(reslutVo, response);
    	return reslutVo;
    }
    
    /**
     * 不安全行为趋势
     * @param entity
     * @return
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getUnfaseBhvTrend")
    @ResponseBody
    public Object getUnfaseBhvTrend(String dateType, String lineId, String carId) throws Exception{
    	Map<String, Object> map = new HashMap<String, Object>();
    	SysUserEntity user = this.getPhoneCurrentUser();
		map.put("dateType", dateType);
		map.put("lineId", lineId);
		map.put("carId", carId);
		map.put("userId", user.getId());
    	List list = phoneDrivingBehaviorService.getUnfaseBhvTrend(map, dateType);
    	return this.getReturnResult(list);
    }
    
    /**
     * 不安全行为类型排名
     * @param vo
     * @return
     * @author:zhangyu
     * @email:zhangyu2222z@sina.cn
     */
    @RequestMapping("/getUnfaseBhvTypeRank")
    @ResponseBody
    public Object getUnfaseBhvTypeRank(DrivingBehaviorParamVo vo){
    	Pagination page = phoneDrivingBehaviorService.getUnfaseBhvTypeRank(vo);
    	GridPage grid = new GridPage(page);
    	return this.getReturnResult(grid);
    }
    
    
}
